thispage=1;
totalpages=0;
//ajks传送
backroleshow(thispage);
optionshow();


function backusershow(name,thispage){
    $.ajax({
        url:"c/backusershow",
        dataType:"json",
        type:"POST",
        data:{
            name:name,
            page:thispage,
        },
        success:function (data){

            show(data);
        },
        error:function (err){
            console.log("传送错误")
        },
    });
}
//下拉框
function optionshow(){
    $.ajax({
        url:"c/backroleoption",
        dataType:"json",
        type:"POST",
        data:{},
        success:function (data){
            console.log(data);
            option(data);
        },
        error:function (err){
            console.log("传送错误")
        },
    });
}
//修改
function upshow(name,account,power,pwd,seq,state,backraleid){
    $.ajax({
        url:"c/backusershow/update",
        dataType:"json",
        type:"POST",
        data:{
            name:name,
            account:account,
            power:power,
            pwd:pwd,
            seq:seq,
            page:thispage,
            state:state,
            backraleid:backraleid,
        },
        success:function (data){
            $('#upshow').modal('hide', 'fit');
            document.getElementById("tiptip").innerText=data;
            $('#tip').modal('toggle', 'center');
            backroleshow(thispage);
        },
        error:function (err){
            console.log("传送错误")
        },
    });
}

//删除
function delshow(seq){
    $.ajax({
        url:"c/backusershow/delete",
        dataType:"json",
        type:"POST",
        data:{
            seq:seq,
        },
        success:function (data){
            $('#delete').modal('hide', 'fit');
            backroleshow(thispage);
        },
        error:function (err){
            console.log("传送错误")
        },
    });
}
//插入
function insertshow(name,pwd,account,role){
    $.ajax({
        url:"c/backusershow/insert",
        dataType:"json",
        type:"POST",
        data:{
            name:name,
            pwd:pwd,
            account:account,
            role:role,
        },
        success:function (data){
            document.getElementById("tiptip").innerText=data;
            $('#tip').modal('toggle', 'center');
            backroleshow(thispage);
            document.getElementById("addname").value="";
            document.getElementById("addcount").value="";
            document.getElementById("addpwd").value="";
        },
        error:function (err){
            console.log("传送错误")
        },
    });
}
//ajks响应区

function show(data){
    var type;
    var type_id;
    var tbody =document.getElementById("tbody");
    var str ='';
    for(var i=0;i<data.list.length;i++){
        let a =data.list[i];
        if(a.stateName==='禁用'){
            type='启用';
            type_id=1;
        }else if(a.stateName==='启用'){
            type="禁用";
            type_id=2;
        }
        str +=`<tr>
      <td>${a.backSeq}</td>
      <td>${a.backAccount}</td>
      <td>${a.backName}</td>
      <td>${a.roleName}</td>
      <td>${a.stateName}</td>
      <td>
      <button class="btn btn-primary btn-sm" onclick="edict(this)">编辑</button>
      <button class="btn btn-danger btn-sm" onclick="eddelte(this)">删除</button>
      <button class="btn btn-warning btn-sm" value="`+type_id+`" onclick="changetype(this)">`+type+`</button>
      <button class="btn btn-primary btn-sm" onclick="chagepwd(this)">密码初始化</button>
      </td>
</tr>`
        totalpages=(data.pages*5);
        var myPager = $('#myPager').data('zui.pager');
        myPager.set({
            page: thispage,
            recTotal: totalpages,
            recPerPage: 5
        });
  }
    tbody.innerHTML=str;
}

function backroleshow(thispage) {
    var name=document.getElementById("searchtext").value;
    backusershow(name,thispage);
}


//编辑页面
function edict(thi){
    document.getElementById("name").value =thi.parentNode.parentNode.children[2].innerHTML;
    document.getElementById("count").value=thi.parentNode.parentNode.children[1].innerHTML;
    document.getElementById("uppanel").value=thi.parentNode.parentNode.children[0].innerHTML;
    $('#upshow').modal('toggle', 'center');
}

function eddelte(thi){
    $('#delete').modal('toggle', 'center');
   document.getElementById("yesdel").value=thi.parentNode.parentNode.children[0].innerHTML;
}

function changetype(thi){
    var seq=thi.parentNode.parentNode.children[0].innerHTML;
    var state=thi.value;
    upshow("","","","",seq,state);
}

function chagepwd(thi){
    var seq=thi.parentNode.parentNode.children[0].innerHTML;
    upshow("","","",888888,seq,"");
}
//option

function option(data){

    var select =document.getElementById("uppower");
    var addselect=document.getElementById("powername");
    var str ='';
    for(var i=0;i<data.length;i++){
        str +=`<option id="`+data[i].backRole+`">`+data[i].backName+`</option>`
    }
    select.innerHTML=str;
    addselect.innerHTML=str;
}


//监听
function sendup(){
    var name=document.getElementById("name").value;
   var account= document.getElementById("count").value;

    var kd1 = document.getElementById('uppower');
    var d1 = kd1.selectedIndex;
    var state = kd1.options[d1].id;
    var seq =document.getElementById("uppanel").value;

    var kd1 = document.getElementById('uppower');
    var d1 = kd1.selectedIndex;
    var role = kd1.options[d1].id;

    upshow(name,account,state,"",seq,"",role);
}
function backuseradd(){
    var name =document.getElementById("addname").value;

    var account =document.getElementById("addcount").value;

    var pwd =document.getElementById("addpwd").value;

    var kd1 = document.getElementById('powername');
    var d1 = kd1.selectedIndex;
    var role = kd1.options[d1].id;

    insertshow(name,pwd,account,role);
}

function yesdelete(){
    var seq= document.getElementById("yesdel").value;
    delshow(seq);
}

// 分页
$('#myPager').on('onPageChange', function(e, state, oldState) {
    if (state.page !== oldState.page) {
        thispage=state.page;
        backroleshow(state.page);
    }
});